package com.breakfast.fun.adapter.home;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.breakfast.fun.R;
import com.breakfast.fun.data.entity.home.Products;
import com.breakfast.fun.util.ImageUtil;

import java.util.List;

/**
 * 商品详情大图适配器
 * Created by Administrator on 2016/10/9.
 */
public class GalleryAdapter extends PagerAdapter {
    //产品列表
    private List<Products> productsList;
    private Context context;
    private boolean isFromGallery = false;
    private String imageIndexStr = "";

    public GalleryAdapter(Context context, List<Products> productsList, boolean isFromGallery) {
        this.context = context;
        this.productsList = productsList;
        this.isFromGallery = isFromGallery;
        if (isFromGallery) {
            imageIndexStr = "http://www.52letsgo.cn";
        }
    }

    @Override
    public int getCount() {
        return productsList == null ? 0 : productsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 创建view
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_home_product, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_home_product);
        ImageUtil imageUtil = new ImageUtil(context);
        imageUtil.loadUrlImage(imageIndexStr + productsList.get(position).getProduct().getBigImage(), imageView);

        imageView.setOnClickListener(v -> {
            ((Activity) context).finish();
        });
        container.addView(view);
        view.setTag(R.string.app_name, position);
        return imageView;
    }

    /**
     * 销毁view
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

    }
}
